package com.ruoyi.project.system.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Anonymous;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.SysProduct;
import com.ruoyi.project.system.domain.SysUserProductPrice;
import com.ruoyi.project.system.mapper.SysProductMapper;
import com.ruoyi.project.system.service.ISysProductService;
import com.ruoyi.project.system.service.ISysUserProductPriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 产品Controller
 */
@RestController
@RequestMapping("/product")
public class SysProductController extends BaseController {
    @Autowired
    private ISysProductService sysProductService;

    @Autowired
    private ISysUserProductPriceService sysUserProductPriceService;

    @Autowired
    private SysProductMapper sysProductMapper;

    /**
     * 获取产品详细信息
     */
    @Anonymous
    @GetMapping("/status")
    public AjaxResult productStatus(int productId) {
        AjaxResult result = new AjaxResult();
        result.putAll(sysProductMapper.getProductStatus(productId));
        return result;
    }

    @GetMapping("/names")
    public AjaxResult distinct() {
        return AjaxResult.success(sysProductMapper.productNameDistinct());
    }

    /**
     * 查询产品权益
     */
    @PostMapping("/productEquity")
    public AjaxResult productEquity(SysProduct sysProduct) {
        sysProduct.setStatus("0");
        sysProduct.setDelFlag("0");
        List<SysProduct> sysProducts = sysProductService.selectSysProductList(sysProduct);
        if (sysProducts.isEmpty()) {
            return AjaxResult.error("暂无产品！");
        }
        return AjaxResult.success(sysProductService.productEquity(sysProducts));
    }

    /**
     * 查询推广权益
     */
    @PostMapping("/promoteEquity")
    public AjaxResult promoteEquity(@RequestBody SysProduct sysProduct) {
        sysProduct.setStatus("0");
        sysProduct.setDelFlag("0");
        List<SysProduct> sysProducts = sysProductService.selectSysProductList(sysProduct);
        if (sysProducts.isEmpty()) {
            return AjaxResult.error("暂无产品！");
        }
        return AjaxResult.success(sysProductService.promoteEquity(sysProducts));
    }

    @Anonymous
    @GetMapping("/priceInfo")
    public AjaxResult priceInfo(Long userId, Integer productId) {
        if (userId == null || productId == null) {
            return AjaxResult.error("参数异常！");
        } else {
            return AjaxResult.success(sysProductService.calculatePriceInfo(userId, productId));
        }
    }

    /**
     * 修改用户产品定价
     */
    @Log(title = "修改用户产品定价", businessType = BusinessType.UPDATE)
    @PutMapping("/setPrice")
    public AjaxResult setUserProductPrice(@RequestBody SysUserProductPrice sysUserProductPrice) {
        if (sysUserProductPrice.getUserId() == null || sysUserProductPrice.getProductId() == null) {
            return AjaxResult.error("参数异常！");
        } else {
            return toAjax(sysUserProductPriceService.setPrice(sysUserProductPrice));
        }
    }

    /**
     * 查询产品列表
     */
    @PreAuthorize("@ss.hasPermi('system:product:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysProduct sysProduct) {
        startPage();
        List<SysProduct> list = sysProductService.selectSysProductList(sysProduct);
        return getDataTable(list);
    }

    /**
     * 导出产品列表
     */
    @PreAuthorize("@ss.hasPermi('system:product:export')")
    @Log(title = "产品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysProduct sysProduct) {
        List<SysProduct> list = sysProductService.selectSysProductList(sysProduct);
        ExcelUtil<SysProduct> util = new ExcelUtil<SysProduct>(SysProduct.class);
        util.exportExcel(response, list, "产品数据");
    }

    /**
     * 获取产品详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:product:query')")
    @GetMapping(value = "/{productId}")
    public AjaxResult getInfo(@PathVariable("productId") int productId) {
        return success(sysProductService.selectSysProductByProductId(productId));
    }

    /**
     * 新增产品
     */
    @PreAuthorize("@ss.hasPermi('system:product:add')")
    @Log(title = "产品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysProduct sysProduct) {
        return toAjax(sysProductService.insertSysProduct(sysProduct));
    }

    /**
     * 修改产品
     */
    @PreAuthorize("@ss.hasPermi('system:product:edit')")
    @Log(title = "产品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysProduct sysProduct) {
        return toAjax(sysProductService.updateSysProduct(sysProduct));
    }
}
